<?php
/*
Bitsand - a web-based booking system for LRP events
Copyright (C) 2006, 2007 Russell Peter Phillips

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

include ('../inc/inc_head_db.php');
include ('../inc/inc_admin.php');
include ('../inc/inc_forms.php');

//Get player ID of player to be edited
$admin_player_id = (int) $_GET ['pid'];
//Initialise error message
$sWarn = '';

$db_prefix = DB_PREFIX;

if ($_POST ['btnSubmit'] != '' && CheckReferrer ('admin/admin_edit_ic.php')) {
	//Character details - check if character exists
	$sql = "SELECT * FROM {$db_prefix}characters WHERE chPlayerID = $admin_player_id";
	$result = ba_db_query ($link, $sql);
	//If character does not exist insert a row so that UPDATE query will work
	if (ba_db_num_rows ($result) == 0) {
		$sql = "INSERT INTO {$db_prefix}characters (chPlayerID) VALUES ($admin_player_id)";
		if (! ba_db_query ($link, $sql)) {
			$sWarn = "There was a problem updating your IC details";
			LogError ("Error inserting player ID into characters table prior to running UPDATE query (admin_edit_ic.php). " .
				"Player ID: $admin_player_id");
		}
	}
	elseif (ba_db_num_rows ($result) > 1)
		LogWarning ("Multiple rows in characters table with player ID (admin_edit_ic.php) $admin_player_id");
	//Build up UPDATE query
	$sql = "UPDATE {$db_prefix}characters SET chName = '" . ba_db_real_escape_string ($link, $_POST ['txtCharName']) . "', " .
		"chRace = '" . ba_db_real_escape_string ($link, $_POST ['txtRace']) . "', " .
		"chCulture = '" . ba_db_real_escape_string ($link, $_POST ['txtCulture']) . "', " .
		"chReason = '" . ba_db_real_escape_string ($link, $_POST ['txtReason']) . "' " .
		"WHERE chPlayerID = $admin_player_id";
	//Run query
	if (! ba_db_query ($link, $sql)) {
		$sWarn = "There was a problem updating your IC details";
		LogError ("Error updating character details (admin_edit_ic.php). Player ID: $admin_player_id");
	}

	//Do not redirect if there are any warnings (all warnings are DB-related)
	if ($sWarn == '') {
		//Make up URL & redirect
		$sURL = fnSystemURL () . "admin/admin_viewdetails.php?pid=$admin_player_id&green=" . urlencode ("IC details updated");
		header ("Location: $sURL");
	}
}

include ('../inc/inc_head_html.php');

//Get existing details if there are any
$sql = "SELECT * FROM {$db_prefix}characters WHERE chPlayerID = $admin_player_id";
$result = ba_db_query ($link, $sql);
$row = ba_db_fetch_assoc ($result);
// Get OOC name
$sql = "SELECT plFirstName, plSurname FROM {$db_prefix}players WHERE plPlayerID = $admin_player_id";
$nameresult = ba_db_query ($link, $sql);
$namerow = ba_db_fetch_assoc ($nameresult);
?>

<h1><?php echo TITLE?> - IC Details</h1>

<?php
if ($sWarn != '')
	echo "<p class = 'warn'>" . $sWarn . "</p>";
?>

<p>
<i>Required fields are <span class = "req_colour">shaded</span></i>. Details will appear on the character card <i>exactly</i> as typed.<br>
</p>

<div class = 'warn'>
<?php
echo "Note that you are editing the IC details for " .
	htmlentities ($namerow ['plFirstName']) . " " . htmlentities ($namerow ['plSurname']) .
	" (" . PID_PREFIX . sprintf ('%03s', $admin_player_id) . ")";
?>
<br>
BE CAREFUL!
</div>

<p>
<form action = "admin_edit_ic.php?pid=<?php echo $admin_player_id?>" method = "post">

<table><tr>
<td>Character Name:</td>
<td><input type = "text" name = "txtCharName" class = "required" value = "<?php echo htmlentities (stripslashes ($row ['chName']))?>"></td>
</tr><tr>
<td>Race:</td>
<td><input type = "text" name = "txtRace" value = "<?php echo htmlentities (stripslashes ($row ['chRace']))?>"></td>
</tr><tr>
<td>Culture:</td>
<td><input type = "text" name = "txtCulture" value = "<?php echo htmlentities (stripslashes ($row ['chCulture']))?>"></td>
</tr>
<tr><td colspan = '2'>&nbsp;</td></tr>
<tr><td colspan = '2'><b>Reason for attending, and any other IC notes:<br>
<textarea rows = "4" cols = "60" name = "txtReason"><?php echo htmlentities (stripslashes ($row ['chReason']))?></textarea>
</td></tr>
<tr><td colspan = '2'>&nbsp;</td></tr>
<tr><td class = 'mid'><input type = 'submit' value = 'Submit' name = 'btnSubmit'></td>
<td class = 'mid'>
<input type = 'reset' value = 'Reset'>
</td></tr>
</table>

</form>

<?php
include ('../inc/inc_foot.php');
?>
